#include "stm32f4xx.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_gpio.h"
#include "misc.h"
#include "stm32f4xx_rcc.h"
// #define ENCODER_USE_REG_CONFIG
void GPIO_Config_func(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_config_s = 
    {
        .GPIO_Mode = GPIO_Mode_AF,
        .GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1,
        .GPIO_PuPd = GPIO_PuPd_UP,
        .GPIO_OType = GPIO_OType_PP,
        .GPIO_Speed = GPIO_Speed_25MHz
    };
    GPIO_Init(GPIOA,&GPIO_config_s);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
    GPIO_config_s.GPIO_Mode = GPIO_Mode_AF;
    GPIO_config_s.GPIO_Pin = GPIO_Pin_6;
    GPIO_config_s.GPIO_OType = GPIO_OType_PP;
    GPIO_Init(GPIOA,&GPIO_config_s);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
}

void NVIC_Config_func(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    NVIC_InitTypeDef NVIC_Config;
    NVIC_Config.NVIC_IRQChannel = TIM6_DAC_IRQn;
    NVIC_Config.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Config.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_Config.NVIC_IRQChannelSubPriority = 2;

    NVIC_Init(&NVIC_Config);
}

void TIM_Config_func(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);

/**********************TIM 2******************************/
    TIM_TimeBaseInitTypeDef TIM_TimeBase = 
    {
        .TIM_ClockDivision = TIM_CKD_DIV1,
        .TIM_CounterMode = TIM_CounterMode_Up,
        .TIM_Period = (0xffffffff),
        .TIM_Prescaler = 0,
        .TIM_RepetitionCounter = 0
    };
    TIM_TimeBaseInit(TIM2, &TIM_TimeBase);
    TIM_ICInitTypeDef IC_Config;

#ifndef ENCODER_USE_REG_CONFIG
    IC_Config.TIM_Channel = TIM_Channel_1;
    IC_Config.TIM_ICFilter = 0;
    IC_Config.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
    IC_Config.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    IC_Config.TIM_ICSelection = TIM_ICSelection_DirectTI;

    TIM_ICInit(TIM2, &IC_Config);
    IC_Config.TIM_Channel = TIM_Channel_2;
    TIM_ICInit(TIM2, &IC_Config);
    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge);
    TIM_SetCounter(TIM2, 0xffffffff>>1);
#else
#define TIMx_CNT(x) (TIM##x->CNT)
#define TIMx_ARR(x) (TIM##x->ARR)
#endif
    TIM_Cmd(TIM2, ENABLE);

/**********************TIM 3******************************/
    TIM_TimeBase.TIM_Prescaler = 9;
    TIM_TimeBase.TIM_Period = 99; 
    TIM_TimeBaseInit(TIM3, &TIM_TimeBase);
    TIM_OCInitTypeDef PWM = 
    {
        .TIM_OCMode = TIM_OCMode_PWM1,
        .TIM_OCIdleState = TIM_OCIdleState_Reset,
        .TIM_OCPolarity = TIM_OCPolarity_High,
        .TIM_OutputState = TIM_OutputState_Enable,
        .TIM_Pulse = 15
    };

    TIM_OC1Init(TIM3, &PWM);
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

/**********************TIM 6******************************/
    TIM_TimeBase.TIM_Prescaler = 83;
    TIM_TimeBase.TIM_Period = 9999; // 10ms
    TIM_TimeBaseInit(TIM6, &TIM_TimeBase);

    TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM6, ENABLE);
    
}

void PeripheralInitialize(void)
{
    NVIC_Config_func();
    GPIO_Config_func();
    TIM_Config_func();
}
